/**
 * Created by Administrator on 2018/1/15.
 */
define(function(require, exports, module) {
    var $=require('zepto');
    cn=require('conment');
    api=require('api');
    module.exports = {
        int:{
            index:0,
            size:0,
            money:cn.getQueryString('money'),

            isLianPaySelf:true,
            lianPayTxt:'',
        },
        selctBank:function () {
            const _this=this;
            cn.Ajax(api.bankcardList,{
                length:9999
            },function(d){
                if(d.flag==1){
                    _this.int.size=d.list.length;
                    if(d.list.length==0){
                        cn.dialog({
                            body:'请先绑定银行卡',
                            cBack:{
                                text:'取消',
                                callBack:function () {
                                    window.location.href='/view/user.html';
                                    return false
                                }
                            },
                            confirm:{
                                text:'去绑定',
                                callBack:function () {
                                    window.location.href='/view/bind_bank.html';
                                    return false;
                                }
                            }

                        })
                    }else{
                       var l=d.list[0].no;
                       var list=d.list;
                       var arr=new Array();
                       $('#cardId').val(d.list[0].id);
                       $('.card_no').text(l.substring(l.length-4,l.length));

                        $.each(list, function (i, l, array) {
                            arr.push('<p class="'+(i==0?'activeSpan':'')+'" data-no="'+l.no+'" data-id="'+l.id+'" data-name="'+l.bankName+'" data-branch="null"> <span>银行卡尾号('+(l.no.substring(l.no.length-4,l.no.length))+')</span> <span></span> </p>')
                        });
                        $('#bank_select_active').append(arr.join(''));
                        $('#bank_select_active p').on('click',function () {
                            $(this).addClass('activeSpan').siblings('p').removeClass('activeSpan');
                            $('#cardId').val($(this).data('id'));
                            $('.card_no').text($(this).data('no').substring($(this).data('no').length-4,$(this).data('no').length));
                        })
                    }
                }
            },'post')
        },

        liansBindBank:function () {
            const _this=this;
            // var data={"agreement_list":[{"bank_code":"01050000","bank_name":"中国建设银行","card_no":"9612","card_type":"2","no_agree":"2018070614574553"}],"count":"1","ret_code":"0000","ret_msg":"交易成功","sign":"joNuAnYboafYxUW17I1f8iy8xbrnsAEGKDg/+hCCjUxnNGgOe98/cmZEoc/rcDHWkkIYCVbE7bxYAloSkH3xLjLKaSF3YnFXh7rYOSEhiujeG34y9jSSNmESAE5dT8HMpNYOR+ELkXOsKn4Czg/DAA2g79+2YNk/mNEJDIpp4uQ=","sign_type":"RSA","user_id":"2007"}
            cn.Ajax(api.lianBindbank,{
                },function(d){
                    var data=d;
                    // if(data.ret_code!=1||data.agreement_list.length==0){
                    //     cn.dialog({
                    //         body:'请先绑定银行卡',
                    //         cBack:{
                    //             text:'取消',
                    //             callBack:function () {
                    //                 window.location.href='/view/user.html';
                    //                 return false;
                    //             }
                    //         },
                    //         confirm:{
                    //             text:'去绑定',
                    //             callBack:function () {
                    //                 window.location.href='/view/bind_bank.html';
                    //                 return false;
                    //             }
                    //         }
                    //
                    //     })
                    // }else{
                        _this.bankcardList(data.agreement_list);
                    // }
            },'post');
        },
        bankcardList:function (t) {
            const _this=this;
            var isMatching=false;
            cn.Ajax(api.bankcardList,{
                length:9999
            },function(d){
                if(d.flag==1){
                    _this.int.size=d.list.length;
                    if(d.list.length==0){
                        cn.dialog({
                            body:'请先绑定银行卡',
                            cBack:{
                                text:'取消',
                                callBack:function () {
                                    window.location.href='/view/user.html';
                                    return false;
                                }
                            },
                            confirm:{
                                text:'去绑定',
                                callBack:function () {
                                    window.location.href='/view/bind_bank.html';
                                    return false;
                                }
                            }

                        })
                    }else{
                        var l=d.list[0].no;
                        var list=d.list;
                        var arr=new Array();
                        var bindL='';
                        var selfL='';
                        // $.each(t, function (b, k, array) {
                        //     bindL+=k.card_no+',';
                            $.each(list, function (i, l, array) {
                                // if(k.card_no==(l.no).substring(l.no.length-4,l.no.length)){
                                    selfL+=l.no.substring(l.no.length-4,l.no.length)+',';
                                    arr.push('<p class="'+(i==0?'activeSpan':'')+'" data-no="'+l.no+'" data-id="'+l.id+'" data-name="'+l.bankName+'" data-branch="null"> <span>银行卡尾号('+(l.no.substring(l.no.length-4,l.no.length))+')</span> <span></span> </p>');
                                    if(!isMatching){
                                        $('#cardId').val(l.id);
                                        $('.card_no').text(l.no.substring(l.no.length-4,l.no.length));
                                        isMatching=true;
                                    }
                                // }
                            });
                        // });
                        _this.int.lianPayTxt=bindL;
                        // if(bindL!=selfL){
                        //     _this.int.isLianPaySelf=false;
                        // }
                        if(!isMatching){
                            cn.dialog({
                                body:'您在连连充值中已绑定尾号：'+bindL+'请绑定该卡，谢谢！',
                                cBack:{
                                    text:'取消',
                                    callBack:function () {
                                        window.location.href='/view/user.html';
                                        return false
                                    }
                                },
                                confirm:{
                                    text:'去绑定',
                                    callBack:function () {
                                        window.location.href='/view/bind_bank.html';
                                        return false
                                    }
                                }

                            })
                        }
                        $('#bank_select_active').append(arr.join(''));
                        $('#bank_select_active p').on('click',function () {
                            $(this).addClass('activeSpan').siblings('p').removeClass('activeSpan');
                            $('#cardId').val($(this).data('id'));
                            var noCard=$(this).data('no').toString();
                            $('.card_no').text(noCard.substring(noCard.length-4,noCard.length));
                        })
                    }
                }
                // console.log(d)
            },'post')
        },
        onlyNumber:function (obj,id){
            var t = obj.charAt(0);
            obj= obj.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
            obj = obj.replace(/^\./g,""); //验证第一个字符是数字
            obj = obj.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
            obj = obj.replace('.','$#$').replace(/\./g,'').replace('$#$','.'); //// //保证.只出现一次，而不能出现两次以上
            $(id).val(obj);
        },

        applyCash:function () {
            var money=$.trim($("#extract_money").val())
            if(money==''||money==null){
                cn.alert('请输入要提现的金额');
                return false
            }else if(parseFloat(money)<=0){
                cn.alert('提现金额需大于0！');
                return false
            }else if(parseFloat(money)>parseFloat($('.money_txt').text())){
                cn.alert('超出最大可用金额');
                return false
            }
            cn.Ajax(api.applyCash,{
                amount:money,
                bankcardId:$('#cardId').val(),
                },function(d){
                    if(d.flag==1){
                        cn.alert(d.message)
                        setTimeout(function () {
                            window.location.href='/view/user.html';
                        },1000)
                    }else{
                        cn.alert(d.message)
                    }
            },'post')
        },

        lianlianPay:function () {
            const _this=this;
            cn.Ajax(api.lianlianPay,{
                amount:_this.int.money,
                bci:$('#cardId').val(),
            },function(d){
                if(d.flag==1){
                    $('#lianlianDataUrl').attr('action',d.req_url);
                    $('#lianlianDateVal').val(d.req_data);
                    $('#lianlianDataUrl').submit();
                   // window.location.href=d.req_url
                }else{
                    cn.alert(d.message)
                }
            },'post')
        },
        init:function () {
            const _this=this;
            if(lianlianExtract){
                _this.liansBindBank();
            }else{
                _this.selctBank();
            }
            $('.lf_close').on('click',function () {
                $('.model_bank').hide();
                $('.model_bank-content').removeClass('fade-enter-active').addClass('fade-leave-active').hide(500)
            });
            $('.rt_confirm').on('click',function () {
                if(_this.int.size>=3){
                    cn.alert('最多可添加3张银行卡，如需更换请联系客服')
                }else{
                    window.location.href='/view/bind_bank.html'
                }
            });
            $('#extract_money').on('input propertychange',function () {
                var v=$(this).val()
                _this.onlyNumber(v,'#extract_money');
                $('#change_money').text((v==""||v==null||isNaN(v))?'0.00':parseFloat(v).toFixed(2))
            });
            $('#allMoney').on('click',function () {
                $('#extract_money').val($('.money_txt').text())
                $('#change_money').text($('.money_txt').text())
            });
            $('#changeMoney_btn').on('click',function () {
                _this.applyCash()
            });
            $('#allBank').on('click',function () {
                if(_this.int.size==0){
                    cn.dialog({
                        body:'添加更多银行卡',
                        close:'取消',
                        confirm:{
                            text:'添加',
                            callBack:function () {
                                window.location.href='/view/bind_bank.html'
                                return false
                            }
                        }

                    })
                }
                /*else if(!_this.int.isLianPaySelf){
                    cn.dialog({
                        body:'您在连连充值中已绑定尾号：'+_this.int.lianPayTxt,
                        close:'取消',
                        confirm:{
                            text:'去添加',
                            callBack:function () {
                                window.location.href='/view/bind_bank.html'
                                return false
                            }
                        }
                    })
                }*/
                else{
                    $('.model_bank').show();
                    $('.model_bank-content').removeClass('fade-leave-active').addClass('fade-enter-active').show()
                }

            });
            $('#deposit_payMoney').text(parseFloat(sessionStorage.getItem('money')).toFixed(2))

            //充值
            $('#lian_btn').on('click',function () {
                _this.lianlianPay()
            });
            $('#pay_lian_money').text(_this.int.money)
        },
    }

});